/*
 *  Queen Mary University of London MOM System
 *  
 *  Copyright Queen Mary University of London
 *  
 *  Authors:
 *  Bob Chew (bob.chew@elec.qmul.ac.uk)
 *  Beatriz Viñal Murciano (beavimu@gmail.com)
 *  John Bigham (john.bigham@elec.qmul.ac.uk)
 *  
 *  
 *  Portions of this software uses Apache Qpid
 *    http://qpid.apache.org
 *  
 *  Apache Qpid license:
 * --------------------------------------------------------------
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * -------------------------------------------------------------
 * 
 */
package users;

import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.qpid.client.message.JMSTextMessage;

/**
 * Listener to messages from the overlay manager.
 */
public class UpdateMessageListener implements MessageListener {
    /** User to which this message listener belongs. */
    private User user;

    /**
     * Getter for the variable 'user'.
     * @return variable 'user'
     */
    public User getUser() {
        return user;
    }

    /**
     * Constructor.
     *
     * @param user  user to which this message listener belongs
     */
    public UpdateMessageListener(final User user) {
        this.user = user;
    }

    /**
     * When an update message from the overlay manager is asynchronously
     * received, update the broker and the port to the ones indicated in the
     * message in the way indicated in the message.
     *
     * Update messages from the overlay manager have the format:
     *         'topic:type of broker that failed:newBroker:newPort'
     *
     * @param msg  received message
     */
    @Override
    public void onMessage(final Message msg) {
        try {

            //get the new data from the message
            String text = ((JMSTextMessage) msg).getText();
            String[] parts = text.split(":");

            if (parts.length == User.NUM_PARTS_FAIL_MSG) {
                String topic = parts[0];
                String type = parts[1]; //primary or mirror
                String newBroker = parts[User.BRK_INDEX_FAIL];
                if (newBroker.equals("null") || newBroker == null) {
                    throw new Exception("No mirror broker assigned");
                }
                int newPort = Integer.parseInt(parts[User.PORT_INDEX_FAIL]);

                if (type.equals("primary")) { //change primary broker
                    getUser().changePrimary(topic, newBroker, newPort);
                } else if (type.equals("mirror")) { //change mirror broker
                    getUser().changeMirror(topic, newBroker, newPort);
                }

            } else {
                throw new Exception("Bad format in message: " + text);
            }

        } catch (Exception e) {
            System.out.println("Error reassigning broker: " + e);
            e.printStackTrace();
        }
    }

}
